package com.kayden.blog.common.advice;

import com.kayden.blog.common.exception.BlogException;
import com.kayden.blog.common.pojo.response.Result;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@Slf4j
@ResponseBody
@ControllerAdvice
public class ExpectionAdvice {


    @ExceptionHandler
    public Result exceptionHandler(Exception e) {
        log.error("发生异常，e:",e);
        return Result.fail(e.getMessage());
    }


    @ExceptionHandler
    public Result exceptionHandler(BlogException e) {
        log.error("发生异常e:{}"+e);
        return Result.fail(e.getErrMsg());
    }


    @ExceptionHandler
    public Result exceptionHandler(HandlerMethodValidationException exception){
        log.error("发生异常, e:{}",exception.getMessage());
        return Result.fail("参数校验失败");
    }


    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentNotValidException exception){
        //TODO 空指针自行处理

        String msg = exception.getBindingResult().getFieldError().getDefaultMessage();
        log.error("发生异常,e{}",exception.getMessage());
        return Result.fail(msg);
    }

}
